package com.googlecode.monitoring.web.controllers;

import com.googlecode.monitoring.dao.StatisticDao;
import com.googlecode.monitoring.web.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * Author: Sergiy Doroshenko
 * Date: Aug 13, 2010
 * Time: 5:53:12 PM
 */
@Controller
public class MonitoringWebController {
    private DbAgentStatusBean dbAgentStatusBean;
    private WsAgentStatusBean wsAgentStatusBean;
    private FlashAgentStatusBean flashAgentStatusBean;
    private WebPageAgentStatusBean webAgentStatusBean;
    private StatisticDao wsStatisticDao;
    private StatisticDao dbStatisticDao;
    private StatisticDao flashStatisticDao;
    private StatisticDao webPageStatisticDao;

    @Resource(name = "dbStatisticDao")
    public void setDbStatisticDao(StatisticDao dbStatisticDao) {
        this.dbStatisticDao = dbStatisticDao;
    }

    @Resource(name = "wsStatisticDao")
    public void setWsStatisticDao(StatisticDao wsStatisticDao) {
        this.wsStatisticDao = wsStatisticDao;
    }

    @Resource(name = "flashStatisticDao")
    public void setFlashStatisticDao(StatisticDao flashStatisticDao) {
        this.flashStatisticDao = flashStatisticDao;
    }



    @Resource(name = "webPageStatisticDao")
    public void setWebPageStatisticDao(StatisticDao webPageStatisticDao) {
        this.webPageStatisticDao = webPageStatisticDao;
    }

    @Autowired
    public void setWebAgentStatusBean(WebPageAgentStatusBean webAgentStatusBean) {
        this.webAgentStatusBean = webAgentStatusBean;
    }

    @Autowired
    public void setDbAgentStatusBean(DbAgentStatusBean dbAgentStatusBean) {
        this.dbAgentStatusBean = dbAgentStatusBean;
    }

	@Autowired
	public void setWsAgentStatusBean(WsAgentStatusBean wsAgentStatusBean) {
		this.wsAgentStatusBean = wsAgentStatusBean;
	}

    @Autowired
    public void setFlashAgentStatusBean(FlashAgentStatusBean flashAgentStatusBean) {
        this.flashAgentStatusBean = flashAgentStatusBean;
    }

    @RequestMapping(value = "/monitoring", method = RequestMethod.GET)
    public ModelAndView monitoring() throws IOException {
        ModelAndView mav = buildMAV("sum", dbAgentStatusBean, "monitoring");

        setTitleToMAV(mav, dbAgentStatusBean.getSettings(), "Web");

        mav.addObject("statistic", dbStatisticDao);
        return mav;
    }

    @RequestMapping(value = "/monitoringDBDetails", method = RequestMethod.GET)
    public ModelAndView monitoringDbDetails() throws IOException {
        ModelAndView mav = buildMAV("db", flashAgentStatusBean, "monitoringDbDetails");

        setTitleToMAV(mav, flashAgentStatusBean.getSettings(), "DB");

        mav.addObject("dbStatistic", dbStatisticDao);
        return mav;
    }

    @RequestMapping(value = "/monitoringWSDetails", method = RequestMethod.GET)
    public ModelAndView monitoringWsDetails() throws IOException {
        ModelAndView mav = buildMAV("ws", flashAgentStatusBean, "monitoringWsDetails");

        setTitleToMAV(mav, flashAgentStatusBean.getSettings(), "WS");

        mav.addObject("wsStatistic", wsStatisticDao);
        return mav;
    }

    @RequestMapping(value = "/monitoringWebDetails", method = RequestMethod.GET)
    public ModelAndView monitoringWebDetails() throws IOException {
        ModelAndView mav = buildMAV("web", webAgentStatusBean, "monitoringWebDetails");

        setTitleToMAV(mav, webAgentStatusBean.getSettings(), "Web");

        mav.addObject("webPageStatistic", dbStatisticDao);
        return mav;
    }

    @RequestMapping(value = "/monitoringFlashDetails", method = RequestMethod.GET)
    public ModelAndView monitoringFlashDetails() throws IOException {
        ModelAndView mav = buildMAV("flash", flashAgentStatusBean, "monitoringFlashDetails");

        setTitleToMAV(mav, flashAgentStatusBean.getSettings(), "Flash");

        mav.addObject("flashStatistic", flashStatisticDao);
        return mav;
    }

    private ModelAndView buildMAV(String detail, StatusBean statusBean, String viewName) throws IOException {
        ModelAndView mav = new ModelAndView();
        mav.addObject("detail", detail);

        mav.addObject("wsItems", wsAgentStatusBean.getData());
        mav.addObject("dbItems", dbAgentStatusBean.getData());
        mav.addObject("flashItems", flashAgentStatusBean.getData());
        mav.addObject("webItems", webAgentStatusBean.getData());

        mav.addObject("dbAgentStatusBean", dbAgentStatusBean);
        mav.addObject("wsAgentStatusBean", wsAgentStatusBean);
        mav.addObject("flashAgentStatusBean", flashAgentStatusBean);
        mav.addObject("webAgentStatusBean", webAgentStatusBean);

		

        SettingsBean settings = statusBean.getSettings();

        mav.setViewName(viewName);

        mav.addObject("autoRefreshInterval", settings.getAutoRefreshInterval());
        mav.addObject("settings", settings);

        return mav;
    }

    private void setTitleToMAV(ModelAndView mav, SettingsBean settings, String name) {
        String title = "Monitoring " + name
                + (settings.getInstanceName() != null && !settings.getInstanceName().isEmpty() ?
                " - " + settings.getInstanceName()
                : ""
        );
        mav.addObject("title", title);
    }
}
